# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=postgresql

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=9.3.4
pkgrel=1
pkgdesc="Libraries for use with PostgreSQL (mingw-w64)"
arch=('any')
url="http://www.postgresql.org/"
license=('custom:PostgreSQL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-libxml2" "${MINGW_PACKAGE_PREFIX}-python2")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
    "${MINGW_PACKAGE_PREFIX}-gettext"
    "${MINGW_PACKAGE_PREFIX}-libxml2"
    "${MINGW_PACKAGE_PREFIX}-openssl"
    "${MINGW_PACKAGE_PREFIX}-readline"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-tcl"
    "${MINGW_PACKAGE_PREFIX}-libxslt"
    "${MINGW_PACKAGE_PREFIX}-python2")
options=('staticlibs' 'strip')
source=("http://ftp.postgresql.org/pub/source/v${pkgver}/postgresql-${pkgver}.tar.bz2"
    postgresql-9.2.4-plperl-mingw.patch
    postgresql-9.3.1-plpython-mingw.patch)
md5sums=('d0a41f54c377b2d2fab4a003b0dac762'
         '6a11681fbd19a3ccf8d41f7fbc9b4392'
         '6401b50c0dee9c963985f0d3f4dbe329')

prepare() {
  cd ${srcdir}/postgresql-$pkgver
  #patch -p1 -i ${srcdir}/postgresql-9.2.4-plperl-mingw.patch
  patch -p1 -i ${srcdir}/postgresql-9.3.1-plpython-mingw.patch
}

build() {
    mkdir -p "${srcdir}/build-${CARCH}" && cd "${srcdir}/build-${CARCH}"
  CFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  CXXFLAGS+=" -D__USE_MINGW_ANSI_STDIO=1"
  ../postgresql-$pkgver/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --with-openssl \
    --enable-thread-safety \
    --enable-integer-datetimes \
    --enable-nls \
    --with-ldap \
    --with-libxml \
    --with-libxslt \
    --with-python \
    --without-perl \
    --with-tcl --with-tclconfig=${MINGW_PREFIX}/lib

  # Make DLL definition file visible during each arch build
  ln -s "${srcdir}/postgresql-$pkgver/src/interfaces/libpq/libpqdll.def" src/interfaces/libpq/
  ln -s "${srcdir}/postgresql-$pkgver/src/interfaces/ecpg/ecpglib/libecpgdll.def" src/interfaces/ecpg/ecpglib/
  ln -s "${srcdir}/postgresql-$pkgver/src/interfaces/ecpg/pgtypeslib/libpgtypesdll.def" src/interfaces/ecpg/pgtypeslib/
  ln -s "${srcdir}/postgresql-$pkgver/src/interfaces/ecpg/compatlib/libecpg_compatdll.def" src/interfaces/ecpg/compatlib/

  #for dir in src/interfaces src/bin/pg_config src/bin/psql src/bin/pg_dump; do
  #  make -C ${dir}
  #done
  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  mkdir -p "${pkgdir}${MINGW_PREFIX}/"{bin,include,lib}

  make DESTDIR="${pkgdir}" install
  # install libs

  #for dir in src/interfaces src/bin/pg_config src/bin/psql src/bin/pg_dump; do
  #  make -C ${dir} DESTDIR="${pkgdir}" install
  #done

  # Move dll's to bin directory
  mv "${pkgdir}${MINGW_PREFIX}/lib/"*.dll "${pkgdir}${MINGW_PREFIX}/bin/"

  # Rename the .a files to .dll.a as they're actually import libraries and not static libraries
  for implib in "${pkgdir}${MINGW_PREFIX}/lib/"*.a; do
    mv $implib ${implib/.a/.dll.a}
  done

  # these headers are needed by the not-so-public headers of the interfaces
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/include/{libpq,postgresql/internal/libpq}  
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/c.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/port.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/postgres_fe.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/"
  install -m644 ${srcdir}/postgresql-${pkgver}/src/include/libpq/pqcomm.h "${pkgdir}${MINGW_PREFIX}/include/postgresql/internal/libpq/"


  # rm -r "$pkgdir${MINGW_PREFIX}/share"
  # rm "$pkgdir${MINGW_PREFIX}/bin/"*.exe

  # these headers are needed by the public headers of the interfaces
  #  install -m644 pg_config.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 pg_config_os.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 pg_config_ext.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  cd "${srcdir}/postgresql-$pkgver/src/include"
  #  install -m644 postgres_ext.h "${pkgdir}${MINGW_PREFIX}/include/"
  #  install -m644 libpq/libpq-fs.h "${pkgdir}${MINGW_PREFIX}/include/libpq/"
  #  install -m644 pg_config_manual.h "${pkgdir}${MINGW_PREFIX}/include/"

}
